package plugin;

import java.awt.Color;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.Timer;

public class TrainAllOutsidePane extends TrainDelayPane {
	private static final long serialVersionUID = 1L;
	private LinkedList<Train> zuglist;
	private int cacheContentCount;
	private Timer refreshTimer;

	public TrainAllOutsidePane(MainUI ui) {
		super(ui);
		this.zuglist = new LinkedList<Train>();
	}

	@Override
	protected void refresh() {
		try {
			update_panel();
			repaint();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void push(int zid, final Train train) {
		if ((train.gleis != null)) {
			// if (!zuglist.contains(train) && (train.gleis!=null)) {
			SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
				@Override
				protected Boolean doInBackground() {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					return true;
				}

				@Override
				protected void done() {
					zuglist.add(train);
				}
			};
			worker.execute();
		}
	}

	public void addPanelToTrain(final Train train) {
		SwingWorker<TrainPaneOutside, Void> worker = new SwingWorker<TrainPaneOutside, Void>() {
			@Override
			protected TrainPaneOutside doInBackground() {
				return new TrainPaneOutside(train, TrainAllOutsidePane.this,
						gridpane);
			}

			@Override
			protected void done() {
				TrainPaneOutside newOne = null;
				try {
					newOne = get();
					train_content_pane.add(newOne);
					newOne.setBackground(new Color(240, 240, 240));
					zugpanellist.add(train);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}
		};
		worker.execute();
	}

	@Override
	public synchronized void repaint() {
		super.revalidate();
		super.repaintSuper();
	}

	@Override
	protected void update_panel() {
		try {
			if (zuglist != null && this.zuglist.size() > this.cacheContentCount) {
				this.cacheContentCount = this.zuglist.size();
				for (Train zug : this.zuglist) {
					if (zug != null) {
						if (!zugpanellist.contains(zug) && !zug.sichtbar) {
							TrainPaneOutside newOne = new TrainPaneOutside(zug,
									this, gridpane);
							zugpanellist.add(zug);
							try {
								train_content_pane.add(newOne,
										zuglist.indexOf(zug));
							} catch (java.lang.IllegalArgumentException e) {
								train_content_pane.add(newOne);
								// System.err.println(zug.name+": Sortierung fehlgeschlagen");
							}
							newOne.setBackground(new Color(240, 240, 240));
							// startFlashing(zug.getTrainPanel());
						}
					}
				}
			} else if (zuglist != null
					&& this.zuglist.size() < this.cacheContentCount) {
				List<Train> tmplist = new LinkedList<Train>();
				tmplist.addAll(zugpanellist);
				for (Train zug : tmplist) {
					if (zug != null) {
						if (!zuglist.contains(zug)) {
							this.remove(zug.getTrainPanel());
						}
					}
				}
				this.cacheContentCount = this.zuglist.size();
			}
		} catch (java.util.ConcurrentModificationException e) {
			e.printStackTrace();
		}
		if (this.refreshTimer != null && !this.refreshTimer.isRunning())
			this.refreshTimer.start();
	}

	public synchronized void removeTrain(int zid, Train train) {
		// if (zuglist.contains(train)) {
		zuglist.remove(train);
		this.cacheContentCount = this.zuglist.size();
		// }
	}

	public synchronized void remove(TrainPane t) {
		zugpanellist.remove(t.getZug());
		zuglist.remove(t.getZug());
		this.cacheContentCount = this.zuglist.size();
		super.remove(t);
	}

	public synchronized void remove(TrainButtonPane t) {
		super.remove(t);
	}

	public JPanel getGridPane() {
		return gridpane;
	}

	@Override
	public void startRemoving(final TrainPane train) {
		setBackground(new Color(240, 240, 240));
	}

	@Override
	public void startFlashing(final TrainPane train) {
		SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
			@Override
			protected Boolean doInBackground() {
				try {
					boolean red = train.getZug().verspaetung >= 0;
					for (int j = 1; j < 5; j++) {
						for (int i = 1; i < 401; i++) {
							train.setBackground(red ? new Color(
									(int) (255 + ((255 - 255) / 400f * i)),
									(int) (115 + ((255 - 115) / 400f * i)),
									(int) (115 + ((255 - 115) / 400f * i)))
									: new Color(
											(int) (255 + ((255 - 255) / 400f * i)),
											(int) (240 + ((255 - 240) / 400f * i)),
											(int) (84 + ((255 - 84) / 400f * i))));
							Thread.sleep(1);
						}
						for (int i = 200; i > 0; i--) {
							train.setBackground(red ? new Color(
									(int) (255 + ((255 - 255) / 200f * i)),
									(int) (115 + ((255 - 115) / 200f * i)),
									(int) (115 + ((255 - 115) / 200f * i)))
									: new Color(
											(int) (255 + ((255 - 255) / 200f * i)),
											(int) (240 + ((255 - 240) / 200f * i)),
											(int) (84 + ((255 - 84) / 200f * i))));
							Thread.sleep(1);
						}
					}
				} catch (InterruptedException e) {

				}
				return true;
			}

			@Override
			protected void done() {

			}
		};
		worker.execute();
	}

	protected void addFromSinglePanel(TrainPaneGreen zug) {
		train_content_pane.add(zug);
		zug.setBackground(new Color(240, 240, 240));
		// startFlashing(zug.getTrainPanel());
	}

	public void resort() {
		Collections.sort(zuglist);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				train_content_pane.removeAll();
				for (Train zug : zuglist) {
					if (zug.getTrainPanelOutside() != null)
						train_content_pane.add(zug.getTrainPanelOutside());
				}
			}
		});

	}
}

//
// package plugin;
//
// import java.awt.Color;
// import java.awt.event.ActionEvent;
// import java.awt.event.ActionListener;
// import java.util.HashMap;
// import java.util.LinkedList;
// import java.util.List;
// import java.util.TreeSet;
//
// import javax.swing.JPanel;
// import javax.swing.SwingWorker;
// import javax.swing.Timer;
//
// public class TrainAllOutsidePane extends TrainDelayPane {
// private static final long serialVersionUID = 1L;
// private HashMap<Integer, Train> zuglist;
// private int cacheContentCount;
// private Timer refreshTimer;
//
// public TrainAllOutsidePane(MainUI ui) {
// super(ui);
// this.zuglist = new HashMap<Integer, Train>();
// this.addAutoRefresh();
//
// }
//
// private void addAutoRefresh() {
// this.refreshTimer = new Timer(1000, new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// TrainAllOutsidePane.this.refresh();
// }
// });
// }
//
// protected void refresh() {
// try {
// repaint();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// public void push(int zid, Train train) {
// if (!zuglist.containsKey(zid) && (train.gleis!=null)) {
// zuglist.put(zid, train);
//
// }
// }
//
// public void removeTrain(int zid, Train train) {
// if (zuglist.containsKey(zid)) {
// zuglist.remove(zid);
// }
//
// }
//
// public void addPanelToTrain(Train train) {
// TrainPaneOutside newOne = new TrainPaneOutside(train, this,
// gridpane);
// this.add(newOne);
// newOne.setBackground(new Color(240,240,240));
// zugpanellist.add(train);
// }
//
// @Override
// public void repaint() {
// try {
// if (zuglist != null && this.zuglist.values().size()>this.cacheContentCount) {
// for (Train zug : this.zuglist.values()) {
// if (zug != null) {
// if (!zugpanellist.contains(zug) && !zug.sichtbar) {
// TrainPaneOutside newOne = new TrainPaneOutside(zug, this,
// gridpane);
// this.add(newOne);
// newOne.setBackground(new Color(240,240,240));
// zugpanellist.add(zug);
// // startFlashing(zug.getTrainPanel());
// }
// }
// }
// this.cacheContentCount=this.zuglist.values().size();
// }else if (zuglist != null &&
// this.zuglist.values().size()<this.cacheContentCount) {
// List<Train> tmplist = new LinkedList<Train>();
// tmplist.addAll(zugpanellist);
// for (Train zug : tmplist) {
// if (zug != null) {
// if (!zuglist.values().contains(zug)) {
// this.remove(zug.getTrainPanel());
// }
// }
// }
// this.cacheContentCount=this.zuglist.values().size();
// }
//
// revalidate();
// super.repaintSuper();
//
// } catch (java.util.ConcurrentModificationException e) {
// e.printStackTrace();
// }
// if(this.refreshTimer!=null && !this.refreshTimer.isRunning())
// this.refreshTimer.start();
// }
//
// public void remove(TrainPane t) {
// zugpanellist.remove(t.getZug());
// super.remove(t);
// }
//
// public void remove(TrainButtonPane t) {
// super.remove(t);
// }
//
// public JPanel getGridPane() {
// return gridpane;
// }
//
// @Override
// public void startRemoving(final TrainPane train) {
// setBackground(new Color(240,240,240));
// }
//
// @Override
// public void startFlashing(final TrainPane train) {
// SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
// @Override
// protected Boolean doInBackground() {
// try {
// boolean red = train.getZug().verspaetung >= 0;
// for (int j = 1; j < 5; j++) {
// for (int i = 1; i < 401; i++) {
// train.setBackground(red ? new Color(
// (int) (255 + ((255 - 255) / 400f * i)),
// (int) (115 + ((255 - 115) / 400f * i)),
// (int) (115 + ((255 - 115) / 400f * i)))
// : new Color(
// (int) (255 + ((255 - 255) / 400f * i)),
// (int) (240 + ((255 - 240) / 400f * i)),
// (int) (84 + ((255 - 84) / 400f * i))));
// Thread.sleep(1);
// }
// for (int i = 200; i > 0; i--) {
// train.setBackground(red ? new Color(
// (int) (255 + ((255 - 255) / 200f * i)),
// (int) (115 + ((255 - 115) / 200f * i)),
// (int) (115 + ((255 - 115) / 200f * i)))
// : new Color(
// (int) (255 + ((255 - 255) / 200f * i)),
// (int) (240 + ((255 - 240) / 200f * i)),
// (int) (84 + ((255 - 84) / 200f * i))));
// Thread.sleep(1);
// }
// }
// } catch (InterruptedException e) {
//
// }
// return true;
// }
//
// @Override
// protected void done() {
//
// }
// };
// worker.execute();
// }
//
// protected void addFromSinglePanel(TrainPaneGreen zug) {
// this.add(zug);
// zug.setBackground(new Color(240,240,240));
// // startFlashing(zug.getTrainPanel());
// }
//
//
//
//
// }
